<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>XML to JSON conversion - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.json.xml2json.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.json.xml2json.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.json.advanced.html">Advanced Usage of Zend_Json</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.json.html">Zend_Json</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.json.server.html">Zend_Json_Server - JSON-RPC server</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.json.xml2json" class="section"><div class="info"><h1 class="title">XML to JSON conversion</h1></div>
    

    <p class="para">
        <span class="classname">Zend_Json</span> provides a convenience method for transforming
        <acronym class="acronym">XML</acronym> formatted data into <acronym class="acronym">JSON</acronym> format. This feature was
        inspired from an <a href="http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/" class="link external">&raquo; 
        IBM developerWorks article</a>.
    </p>

    <p class="para">
        <span class="classname">Zend_Json</span> includes a static function called
         <span class="methodname">Zend_Json::fromXml()</span>. This function will generate
        <acronym class="acronym">JSON</acronym> from a given <acronym class="acronym">XML</acronym> input. This function takes any
        arbitrary <acronym class="acronym">XML</acronym> string as an input parameter. It also takes an optional
        boolean input parameter to instruct the conversion logic to ignore or not ignore the
        <acronym class="acronym">XML</acronym> attributes during the conversion process. If this optional input
        parameter is not given, then the default behavior is to ignore the <acronym class="acronym">XML</acronym>
        attributes. This function call is made as shown below:
    </p>

    <pre class="programlisting brush: php">
// fromXml function simply takes a String containing XML contents
// as input.
$jsonContents = Zend_Json::fromXml($xmlStringContents, true);
</pre>


    <p class="para">
         <span class="methodname">Zend_Json::fromXml()</span> function does the conversion of the
        <acronym class="acronym">XML</acronym> formatted string input parameter and returns the equivalent
        <acronym class="acronym">JSON</acronym> formatted string output. In case of any <acronym class="acronym">XML</acronym> input
        format error or conversion logic error, this function will throw an exception. The
        conversion logic also uses recursive techniques to traverse the <acronym class="acronym">XML</acronym> tree.
        It supports recursion upto 25 levels deep. Beyond that depth, it will throw a
        <span class="classname">Zend_Json_Exception</span>. There are several <acronym class="acronym">XML</acronym> files
        with varying degree of complexity provided in the tests directory of Zend Framework. They
        can be used to test the functionality of the xml2json feature.
    </p>

    <p class="para">
        The following is a simple example that shows both the <acronym class="acronym">XML</acronym> input string
        passed to and the <acronym class="acronym">JSON</acronym> output string returned as a result from the
         <span class="methodname">Zend_Json::fromXml()</span> function. This example used the optional
        function parameter as not to ignore the <acronym class="acronym">XML</acronym> attributes during the
        conversion. Hence, you can notice that the resulting <acronym class="acronym">JSON</acronym> string includes
        a representation of the <acronym class="acronym">XML</acronym> attributes present in the
        <acronym class="acronym">XML</acronym> input string.
    </p>

    <p class="para">
        <acronym class="acronym">XML</acronym> input string passed to  <span class="methodname">Zend_Json::fromXml()</span>
        function:
    </p>

    <pre class="programlisting brush: php">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;books&gt;
    &lt;book id=&quot;1&quot;&gt;
        &lt;title&gt;Code Generation in Action&lt;/title&gt;
        &lt;author&gt;&lt;first&gt;Jack&lt;/first&gt;&lt;last&gt;Herrington&lt;/last&gt;&lt;/author&gt;
        &lt;publisher&gt;Manning&lt;/publisher&gt;
    &lt;/book&gt;

    &lt;book id=&quot;2&quot;&gt;
        &lt;title&gt;PHP Hacks&lt;/title&gt;
        &lt;author&gt;&lt;first&gt;Jack&lt;/first&gt;&lt;last&gt;Herrington&lt;/last&gt;&lt;/author&gt;
        &lt;publisher&gt;O&#039;Reilly&lt;/publisher&gt;
    &lt;/book&gt;

    &lt;book id=&quot;3&quot;&gt;
        &lt;title&gt;Podcasting Hacks&lt;/title&gt;
        &lt;author&gt;&lt;first&gt;Jack&lt;/first&gt;&lt;last&gt;Herrington&lt;/last&gt;&lt;/author&gt;
        &lt;publisher&gt;O&#039;Reilly&lt;/publisher&gt;
    &lt;/book&gt;
&lt;/books&gt;
</pre>


    <p class="para">
        <acronym class="acronym">JSON</acronym> output string returned from
         <span class="methodname">Zend_Json::fromXml()</span> function:
    </p>

    <pre class="programlisting brush: php">
{
   &quot;books&quot; : {
      &quot;book&quot; : [ {
         &quot;@attributes&quot; : {
            &quot;id&quot; : &quot;1&quot;
         },
         &quot;title&quot; : &quot;Code Generation in Action&quot;,
         &quot;author&quot; : {
            &quot;first&quot; : &quot;Jack&quot;, &quot;last&quot; : &quot;Herrington&quot;
         },
         &quot;publisher&quot; : &quot;Manning&quot;
      }, {
         &quot;@attributes&quot; : {
            &quot;id&quot; : &quot;2&quot;
         },
         &quot;title&quot; : &quot;PHP Hacks&quot;, &quot;author&quot; : {
            &quot;first&quot; : &quot;Jack&quot;, &quot;last&quot; : &quot;Herrington&quot;
         },
         &quot;publisher&quot; : &quot;O&#039;Reilly&quot;
      }, {
         &quot;@attributes&quot; : {
            &quot;id&quot; : &quot;3&quot;
         },
         &quot;title&quot; : &quot;Podcasting Hacks&quot;, &quot;author&quot; : {
            &quot;first&quot; : &quot;Jack&quot;, &quot;last&quot; : &quot;Herrington&quot;
         },
         &quot;publisher&quot; : &quot;O&#039;Reilly&quot;
      }
   ]}
}
</pre>


<div class="section" id="zend.json.xml2json.changes"><div class="info"><h1 class="title">Changes</h1></div>
    

    <div class="section" id="zend.json.xml2json.changes.1-11-6"><div class="info"><h1 class="title">Changes in 1.11.6</h1></div>
        

        <p class="para">
            Starting from the release 1.11.6 the  <span class="methodname">Zend_Json::fromXml()</span> function
            has been rewritten from scratch in order to manage XML element with attributes, text value
            and sub-elements (see the <a href="http://framework.zend.com/issues/browse/ZF-3257" class="link external">&raquo; ZF-3257</a>).
        </p>

        <p class="para">
            For instance, if you have an XML document like this:
        </p>

        <pre class="programlisting brush: php">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;a&gt;
    &lt;b id=&quot;foo&quot;/&gt;
    bar
&lt;/a&gt;
</pre>


            <p class="para">
                The <acronym class="acronym">JSON</acronym> output string returned from
                 <span class="methodname">Zend_Json::fromXml()</span> is:
            </p>

            <pre class="programlisting brush: php">
{
   &quot;a&quot; : {
      &quot;b&quot; : {
         &quot;@attributes&quot; : {
            &quot;id&quot; : &quot;foo&quot;
         }
      },
      &quot;@text&quot; : &quot;bar&quot;
   }
}
</pre>


            <p class="para">
                The idea is to use a special key value (@text) to store the text value of an XML element,
                only if this element contains attributes or sub-elements (as in the previous examples).
                If you have a simple XML element with only a text value, like this:
            </p>

            <pre class="programlisting brush: php">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;a&gt;foo&lt;/a&gt;
</pre>


            <p class="para">
                the JSON will be {&quot;a&quot;:&quot;foo&quot;} that is quite intuitive, instead of {&quot;a&quot;:{&quot;@text&quot;:&quot;foo&quot;}}.
            </p>
    </div>
</div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.json.advanced.html">Advanced Usage of Zend_Json</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.json.html">Zend_Json</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.json.server.html">Zend_Json_Server - JSON-RPC server</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.json.html">Zend_Json</a></li>
  <li><a href="zend.json.introduction.html">Introduction</a></li>
  <li><a href="zend.json.basics.html">Basic Usage</a></li>
  <li><a href="zend.json.advanced.html">Advanced Usage of Zend_Json</a></li>
  <li class="active"><a href="zend.json.xml2json.html">XML to JSON conversion</a></li>
  <li><a href="zend.json.server.html">Zend_Json_Server - JSON-RPC server</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>